package com.example.boardroom.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ServiceUtils;
import com.example.boardroom.R;
import com.example.boardroom.demo.room.AppDatabase;
import com.example.boardroom.demo.room.TimesDao;
import com.example.boardroom.demo.times.HardwareService;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "hello";
    TextView tvtv2;
    Button btn_query,btn_delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: hello ===>MainActivity启动了");
        tvtv2 = findViewById(R.id.tvtv2);
        btn_query=findViewById(R.id.btn_query);
        btn_delete=findViewById(R.id.btn_delete);
        RegisterBroadCastReceiver();
        initDB();
        if (!ServiceUtils.isServiceRunning(HelloService.class)) {
            ServiceUtils.startService(HelloService.class);
        }
        if (!ServiceUtils.isServiceRunning(HardwareService.class)) {
            ServiceUtils.startService(HardwareService.class);
        }
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        LogUtils.i("lufei_room",timesDao.TimesTable());
                    }
                }).start();

            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        timesDao.clear();
                    }
                }).start();
            }
        });
    }

    private TimesDao timesDao;

    private void initDB() {
        timesDao = AppDatabase.getInstance(this).userDao();
    }

    PowerReceiver mPowerReceiver = null;

    private void RegisterBroadCastReceiver() {
        if (mPowerReceiver != null) {
            UnRegisterBroadCastReceiver();
        }
        mPowerReceiver = new PowerReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.ACTION_SHUTDOWN");
        intentFilter.addAction("android.intent.action.REBOOT");
        registerReceiver(mPowerReceiver, intentFilter);
    }

    private void UnRegisterBroadCastReceiver() {
        if (mPowerReceiver != null) {
            unregisterReceiver(mPowerReceiver);
            mPowerReceiver = null;
        }
    }

    private void xxx() {
        OkGo.<String>post("http://apis.juhe.cn/simpleWeather/query")
                .tag(this)
                .params("city", "北京")
                .params("key", "16c6a6c8fc76968f8b41924a9cc00f99")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.i("lufei", "onSuccess");
                        tvtv2.setText(response.body());

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        LogUtils.i("lufei", "onError");
                    }

                    @Override
                    public void onFinish() {
                        LogUtils.i("lufei", "onFinish");
                        super.onFinish();
                    }
                });
    }
}
